package com.yidu.ruifeng_logistics.service.impl;

import com.yidu.ruifeng_logistics.dao.LogisticsUserRoleDao;
import com.yidu.ruifeng_logistics.entity.LogisticsStaff;
import com.yidu.ruifeng_logistics.dao.LogisticsStaffDao;
import com.yidu.ruifeng_logistics.entity.LogisticsUserRole;
import com.yidu.ruifeng_logistics.service.LogisticsStaffService;
import com.yidu.ruifeng_logistics.vo.LogisticsStaffVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

/**
 * (LogisticsStaff)表服务实现类
 *
 * @author 张皓超
 * @since 2021-03-09 14:29:19
 */
@Service("logisticsStaffService")
public class LogisticsStaffServiceImpl implements LogisticsStaffService {
    @Resource
    private LogisticsStaffDao logisticsStaffDao;

    @Resource
    private LogisticsUserRoleDao logisticsUserRoleDao;

    /**
     * 通过ID查询单条数据
     *
     * @param staffId 主键
     * @return 实例对象
     */
    @Override
    public LogisticsStaff queryById(Integer staffId) {
        return this.logisticsStaffDao.queryById(staffId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogisticsStaff> queryAllByLimit(int offset, int limit) {
        return this.logisticsStaffDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param logisticsStaff 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsStaff insert(LogisticsStaff logisticsStaff) {
        this.logisticsStaffDao.insert(logisticsStaff);
        return logisticsStaff;
    }

    /**
     * 修改数据
     *
     * @param logisticsStaff 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsStaff update(LogisticsStaff logisticsStaff) {
        this.logisticsStaffDao.update(logisticsStaff);
        return this.queryById(logisticsStaff.getStaffId());
    }

    /**
     * 通过主键删除数据
     *
     * @param staffId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer staffId) {
        return this.logisticsStaffDao.deleteById(staffId) > 0;
    }

    @Override
    public List<LogisticsStaff> queryAll(LogisticsStaff logisticsStaff) {
        return this.logisticsStaffDao.queryAll(logisticsStaff);
    }

    @Override
    public int queryUserNameAndPwdReturnCount(String userName, String userPwd) {
        return this.logisticsStaffDao.queryUserNameAndPwdReturnCount(userName, userPwd);
    }

    @Override
    public LogisticsStaff queryUserNameAndPwdReturnObj(String userName, String userPwd) {
        return this.logisticsStaffDao.queryUserNameAndPwdReturnObj(userName, userPwd);
    }

    @Override
    public Integer queryUserNameAndIdReturnCount(String staffName, Integer staffId) {
        return logisticsStaffDao.queryUserNameAndIdReturnCount(staffName, staffId);
    }

    @Override
    public Integer addAndUpdate(LogisticsStaffVo vo, String suserRoleId) {
        LogisticsStaff po = new LogisticsStaff();
        if (!StringUtils.isEmpty(vo.getStaffBirthday_())) {
            po.setStaffBirthday(LocalDate.parse(vo.getStaffBirthday_()));
        }
        if (!StringUtils.isEmpty(vo.getStaffSlary())) {
            po.setStaffSlary(Double.valueOf(vo.getStaffSlary()));
        }
        BeanUtils.copyProperties(vo, po); //copy
        LogisticsUserRole logisticsUserRole = new LogisticsUserRole();


        int i = 0;
        if (vo.getStaffId() != null) {
            //修改
            i=logisticsStaffDao.update(po);
            if (suserRoleId != null && suserRoleId != "") {
                String[] split = suserRoleId.split(",");
                logisticsUserRoleDao.deleteStaffId(po.getStaffId());
                i = logisticsUserRoleDao.insertAll(split, po.getStaffId());
            }
        } else {
            //增加
            i=logisticsStaffDao.insert(po);
            if (suserRoleId != null && suserRoleId != "") {
                String[] split = suserRoleId.split(",");
                logisticsUserRole.setStaffId(po.getStaffId());
                i = logisticsUserRoleDao.insertAll(split, po.getStaffId());
            }
        }

        return i;
    }

    @Override
    public LogisticsStaff findUserByUsername(String username) {
        return this.logisticsStaffDao.findUserByUsername(username);
    }

}
